package com.lzxuni.modules.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.web.HttpContextUtils;
import com.lzxuni.modules.basics.service.BasicsLandService;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.cropinformation.service.TblCropInformationService;
import com.lzxuni.modules.disease.entity.DiseaseManagement;
import com.lzxuni.modules.disease.service.DiseaseManagementService;
import com.lzxuni.modules.pestdetails.entity.Pest;
import com.lzxuni.modules.pestdetails.service.PestService;
import com.lzxuni.modules.weather.entity.WeatherInfo;
import com.lzxuni.modules.weather.service.WeatherForecastService;
import com.lzxuni.modules.weather.service.WeatherInfoHourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class DesktopController extends BaseController {

    //气象站小时采集数据
    @Autowired
    private WeatherInfoHourService weatherInfoHourService;
    //耕地面积
    @Autowired
    private BasicsLandService basicsLandService;
    //作物及耕种面积
    @Autowired
    private TblCropInformationService tblCropInformationService;


    //预警信息
    @Autowired
    private WeatherForecastService weatherForecastService;

    //虫情
    @Autowired
    private PestService pestService;
    //病情
    @Autowired
    private DiseaseManagementService diseaseManagementService;




    // 主页包含内容页
    @RequestMapping("/Home/AdminDesktop")

    public ModelAndView adminDesktopTemp(String username) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        ModelAndView mv = new ModelAndView("/Home/desktop");

System.out.println("==================");
        //查询天气
        WeatherInfo weatherInfo = weatherInfoHourService.getOne(new QueryWrapper<WeatherInfo>().eq("time_mark", '0').orderByDesc("create_date"));
        mv.addObject("weatherInfo", weatherInfo);
        //查询总耕地面积
        mv.addObject("landArea",basicsLandService.queryCount());
        mv.addObject("cropInforCountList",tblCropInformationService.queryCountList());


        List<WeatherInfo> weatherInfoList= weatherForecastService.queryDesktopList();
        if(weatherInfoList!=null&&weatherInfoList.size()>0){
            mv.addObject("weatherForecast",weatherInfoList.get(0));
            mv.addObject("weatherForecastList",weatherInfoList);

        }

        List<Pest> pestList= pestService.queryYuJing();
        if(pestList!=null&&pestList.size()>0){
//            pestList.forEach(System.out::println);
            mv.addObject("pestInfo",pestList.get(0));
            mv.addObject("pestList",pestList);

        }


        List<DiseaseManagement> diseaseManagementList= diseaseManagementService.queryListThree();
        if(diseaseManagementList!=null&&diseaseManagementList.size()>0){
//            diseaseManagementList.forEach(System.out::println);
            mv.addObject("diseaseManagementInfo",diseaseManagementList.get(0));
            mv.addObject("diseaseManagementList",diseaseManagementList);

        }



        return mv;
    }


}
